package com.nuanshui.heatedloan.baseservice.service;

import com.nuanshui.framework.res.ResourceFacade;
import com.nuanshui.framework.res.qiniu.Qiniu;
import com.nuanshui.framework.res.qiniu.impl.QiniuNonProdImpl;
import com.nuanshui.heatedloan.baseservice.facade.Image2MediaImageConvert;
import com.nuanshui.heatedloan.baseservice.facade.ImageService;
import com.nuanshui.heatedloan.baseservice.form.MediaForm;
import com.nuanshui.heatedloan.baseservice.model.Media;
import com.nuanshui.heatedloan.baseservice.vo.MediaVO;
import com.nuanshui.heatedloan.dal.vo.AdvertisementVO;
import com.nuanshui.heatedloan.enums.FileBelong;
import com.nuanshui.heatedloan.model.Image;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by wangxw on 2017/6/28.
 */
@Service("image2MediaImageConvert")
public class Image2MediaImageConvertImpl implements Image2MediaImageConvert {

    @Autowired
    private ResourceFacade resourceFacade;

    @Autowired
    private Qiniu qiniuNonProd;

    @Autowired
    private ImageService imageService;

    @Override
    public MediaVO convertAdvertisementVO2MediaImageVO(AdvertisementVO advertisementVO){
        Image image = null;
        if(advertisementVO != null){
            String imageId = advertisementVO.getImageId();
            image = imageService.load(imageId);
        }
        return this.convert(image, null);

    }


    @Override
    public MediaVO convert(Image image, Object param) {
        MediaVO media = new MediaVO();
        if(image != null){
            media.setType(1);
            media.setBelong(image.getBelong());
            media.setCreatedAt(image.getCreatedAt());
            media.setCropHeight(image.getCropHeight() == null ? null : image.getCropHeight().intValue());
            media.setCropWidth(image.getCropWidth() == null ? null : image.getCropWidth().intValue());
            media.setCropx(image.getCropX() == null ? null : image.getCropX() .intValue());
            media.setCropy(image.getCropY() == null ? null : image.getCropY().intValue());
            media.setHeight(image.getHeight());
            media.setImgKey(image.getKey());
            media.setUpdatedAt(image.getUpdatedAt());
            media.setWidth(image.getWidth());
        }
        return media;
    }

    @Override
    public Image reConvert(Media media, Object param) {

        Image image = new Image();
        if(media.getType() == 1){
            image.setBelong(media.getBelong());
            image.setCreatedAt(media.getCreatedAt());
            image.setCropHeight(media.getCropHeight() == null ? null : media.getCropHeight().doubleValue());
            image.setCropWidth(media.getCropWidth() == null ? null : media.getCropWidth().doubleValue());
            image.setCropX(media.getCropx() == null ? null : media.getCropx().doubleValue());
            image.setCropY(media.getCropy() == null ? null : media.getCropy().doubleValue());
            image.setHeight(media.getHeight());
            image.setImgUrl(media.getImgKey() == null ? null : resourceFacade.resolveUrl(media.getImgKey()));
            image.setKey(media.getImgKey());
            image.setUpdatedAt(media.getUpdatedAt());
            image.setWidth(media.getWidth());
        }

        return image;
    }


    @Override
    public Image mediaFromImage2Image(MediaForm form) {
        Image image = reConvert(form, null);
        String bucketName = qiniuNonProd.genBucketName(FileBelong.PRODUCT);
        image.setKey(image.getKey() == null ? null : qiniuNonProd.qiniuKeyToLocalKey(image.getKey(), bucketName));
        image.setBelong(FileBelong.PRODUCT);
        return image;
    }


    @Override
    public Media convert(Image s, Object param, Media t) {
        return null;
    }

    @Override
    public Image reConvert(Media t, Object param, Image s) {
        return null;
    }

}
